package com.demo.service.impl;


import java.util.List;

import com.demo.entity.constants.Constants;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.DeviceInventory;
import com.demo.entity.po.Laboratory;

import com.demo.entity.po.SysAccount;
import com.demo.entity.query.DeviceInventoryQuery;
import com.demo.entity.query.LaboratoryQuery;

import com.demo.entity.query.SysAccountQuery;
import com.demo.entity.vo.PaginationResultVO;

import com.demo.exception.BusinessException;
import com.demo.mappers.DeviceInventoryMapper;
import com.demo.mappers.SysAccountMapper;
import com.demo.service.LaboratoryService;

import org.aspectj.apache.bcel.classfile.Constant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.demo.entity.query.SimplePage;

import com.demo.mappers.LaboratoryMapper;

import com.demo.entity.enums.PageSize;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description:serviceImpl
 * @Date:2025-03-11
 * @author：author
*/
@Service("laboratoryService")
public class LaboratoryServiceImpl implements LaboratoryService{

	@Resource
	private LaboratoryMapper<Laboratory,LaboratoryQuery> laboratoryMapper;

	@Resource
	private SysAccountMapper<SysAccount, SysAccountQuery> sysAccountMapper;

	@Resource
	private DeviceInventoryMapper<DeviceInventory, DeviceInventoryQuery> deviceInventoryMapper;


	/**
	 * 根据条件查询列表
	 */
	public List<Laboratory> findListByParam(LaboratoryQuery query) { 
		return this.laboratoryMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(LaboratoryQuery query){
		return this.laboratoryMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<Laboratory> findListByPage(LaboratoryQuery  query) { 
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize()==null?PageSize.SIZE15.getSize():query.getPageSize();
		SimplePage page = new SimplePage(query.getPageNo(),count,pageSize);
		query.setSimplePage(page);
		List<Laboratory> list = this.findListByParam(query);
		return new PaginationResultVO(count,page.getPageSize(),page.getPageNo(),page.getPageTotal(),list);
	}

	/**
	 * 新增
	 */
	public Integer add(Laboratory bean) { 
		return this.laboratoryMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<Laboratory> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.laboratoryMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	public Integer addOrUpdateBatch(List<Laboratory> listBean) { 
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.laboratoryMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据LaboratoryId查询
	 */
	public Laboratory getLaboratoryByLaboratoryId(Integer laboratoryId) { 
		return this.laboratoryMapper.selectByLaboratoryId(laboratoryId);
	}

	/**
	 * 根据LaboratoryId更新
	 */
	public Integer updateLaboratoryByLaboratoryId(Laboratory bean, Integer laboratoryId) { 
		return this.laboratoryMapper.updateByLaboratoryId(bean,laboratoryId);
	}

	/**
	 * 根据LaboratoryId删除
	 */
	@Transactional(rollbackFor = Exception.class)
	public Integer deleteLaboratoryByLaboratoryId(Integer laboratoryId) {
		Laboratory laboratoryDB = laboratoryMapper.selectByLaboratoryId(laboratoryId);
		if (laboratoryDB == null) {
			throw new BusinessException("实验室不存在,或已经被删除");
		}
		// 实验室存放有实验设备信息 不允许删除
		DeviceInventoryQuery inventoryQuery = new DeviceInventoryQuery();
		inventoryQuery.setWarehouse(laboratoryDB.getName());
		Integer count = deviceInventoryMapper.selectCount(inventoryQuery);
		if (count >= Constants.ONE) {
			throw new BusinessException("该实验室中含有实验器材库存信息，不允许删除");
		}
		return this.laboratoryMapper.deleteByLaboratoryId(laboratoryId);
	}

	/**
	 * 保存实验室信息
	 * @param bean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveLaboratory(Laboratory bean) {
		SysAccount sysAccountDB=sysAccountMapper.selectByUserId(bean.getUserId());;
		if (sysAccountDB==null){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		// 执行保存或修改逻辑
		if (bean.getLaboratoryId()==null){
			laboratoryMapper.insert(bean);
		}else {
			laboratoryMapper.updateByLaboratoryId(bean,bean.getLaboratoryId());
		}

		LaboratoryQuery laboratoryQuery = new LaboratoryQuery();
		laboratoryQuery.setName(bean.getName());
		Integer count = laboratoryMapper.selectCount(laboratoryQuery);
		if (count>=Constants.TWO){
			throw new BusinessException("实验室名称重复，请重新添加");
		}
	}
}